#!/usr/bin/python3
# -*-coding:utf-8 -*-
"""
@Date   : 2019/11/15 14:59
@Author : jiangwm
@Desc   : 
"""
import os
import sys
os.chdir('../..')
sys.path.insert(0, os.getcwd())
from retrying import retry
from quant_researcher.quant.datasource_fetch.basic_model.requests_model import MyRequest
import random
import datetime
import time
import json
import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

class sh_gold_datas:
    def __init__(self):
        self.myRequest = None

        self.headers = {
            'Accept': '*/*',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
            'Cache-Control': 'no-cache',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'DNT': '1',
            'Host': 'www.sge.com.cn',
            'Origin': 'https://www.sge.com.cn',
            'Pragma': 'no-cache',
            'Referer': 'https://www.sge.com.cn/sjzx/mrhq',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest'
        }

    def ready_info(self):
        self.myRequest = MyRequest()

    @retry(stop_max_attempt_number=3)
    def sh_gold_trans(self):
        self.ready_info()

        try:
            aim_insert_list = []
            cookies_url = "https://www.sge.com.cn/sjzx/mrhq"
            get_cookies = self.myRequest.get_cookie(cookies_url)

            sh_gold_dict = self.sh_gold_info()

            for shGold_item in sh_gold_dict.keys():
                index_name = sh_gold_dict[shGold_item]

                post_json = {
                    "instid": shGold_item,
                }

                index_url = "https://www.sge.com.cn/graph/Dailyhq"
                response_content = self.myRequest.post_dataJson_cookies(index_url, self.headers, post_json, get_cookies)
                response_content_temp = response_content.replace("'", '"')
                response_content_dict = json.loads(response_content_temp)

                if response_content_dict:
                    index_list = response_content_dict["time"]
                    for indexItem in index_list:
                        trade_date_temp = indexItem[0]
                        trade_date = datetime.datetime.strptime(trade_date_temp, "%Y-%m-%d").strftime('%Y%m%d')
                        open = indexItem[1]
                        high = indexItem[4]
                        low = indexItem[3]
                        close = indexItem[2]

                        # time
                        titime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                        aim_data = {
                            "index_code": shGold_item,
                            "index_name": index_name,
                            "trade_date": trade_date,
                            "open": open,
                            "high": high,
                            "low": low,
                            "close": close,
                        }

                        aim_query_count = self.mongodb_index_center.find(self.aim_collection, {
                            "index_code": aim_data["index_code"], "trade_date": trade_date}).count()

                        if aim_query_count < 1:
                            aim_insert_dict = aim_data.copy()
                            aim_insert_dict["titime"] = titime
                            aim_insert_list.append(aim_insert_dict)

                        if aim_insert_list:
                            if len(aim_insert_list) % 2000 == 0:
                                self.chinaFuture_future_log.info(
                                    'insert_batch:{aim}, {insert_batch}'.format(aim=aim_data["index_name"],
                                                                                insert_batch=len(aim_insert_list)))
                                try:
                                    self.mongodb_index_center.insertMany(self.aim_collection, aim_insert_list)
                                except Exception as err:
                                    self.chinaFuture_future_log.info("Fault:{err}".format(err=err))
                                    continue
                                finally:
                                    aim_insert_list = []
                    try:
                        if aim_insert_list:
                            self.chinaFuture_future_log.info(
                                'insertAim：{aim}---insert:{insert}'.format(aim=aim_insert_list[0]['index_name'],
                                                                           insert=len(aim_insert_list)))
                            try:
                                self.mongodb_index_center.insertMany(self.aim_collection, aim_insert_list)
                            except Exception as err:
                                self.chinaFuture_future_log.info("Fault:{err}".format(err=err))
                                continue
                            finally:
                                aim_insert_list = []
                    except Exception as err:
                        self.chinaFuture_future_log.info("Fault:{err}".format(err=err))
                    finally:
                        pass
            print("sh_gold_trans is over...")
        except Exception as err:
            print("Fault: err: '%s'" % err)
        finally:
            pass

    def sh_gold_info(self):
        sh_gold = {
            "Au99.95": "上海黄金Au99.95",
            "Au99.99": "上海黄金Au99.99",
            "Au100g": "上海黄金Au100g",
            "Pt99.95": "上海黄金Pt99.95",
            "Au(T+D)": "上海黄金Au(T+D)",
            "Ag(T+D)": "上海黄金Ag(T+D)",
            "mAu(T+D)": "上海黄金mAu(T+D)",
            "Au(T+N1)": "上海黄金Au(T+N1)",
            "Au(T+N2)": "上海黄金Au(T+N2)",
            "Ag99.99": "上海黄金Ag99.99",
            "Ag99.9": "上海黄金Ag99.9",
            "iAu99.99": "上海黄金iAu99.99",
            "Au99.5": "上海黄金Au99.5",
            "iAu100g": "上海黄金iAu100g",
            "iAu99.5": "上海黄金iAu99.5",
            "Au50g": "上海黄金Au50g",
            "PGC30g": "上海黄金PGC30g",
            "NYAuTN06": "沪纽金TN06",
            "NYAuTN12": "沪纽金TN12",
        }
        return sh_gold


if __name__ == '__main__':
    result_sws_data = sh_gold_datas()
    result_sws_data.sh_gold_trans()